﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Utility;
using Entity;
using DataAccess.IService;


namespace DataAccess.Service
{
    public class AnnouncementService
    {
        //8个
        public bool insertAnnoucement(Announcement announcement)
        {
            bool result = false;
            string commandText = "Insert into Announcement(title,author,content) values(@title,@author,@content)";
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("title", announcement.Title),
                new SqlParameter("author", announcement.Athour),
                new SqlParameter("content", announcement.Content)        
            };
            if ( SQLHelper.ExecuteNonQuery(commandText, paras)>0)
            {
                result = true;
            }
            return result;
        }

        public DataSet selectAnnouncementDataSet()
        {
            string commandText = "select * from Announcement";
            return SQLHelper.ExecuteDataSet(commandText);
        }


        public Announcement selectAnnouncement(int id)
        {
            Announcement announcement = new Announcement();
            string commandText = "select * from Announcement where id=@id";
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("id",id)
            };
            SqlDataReader sdReader = SQLHelper.ExecuteReader(commandText, paras);
            if (sdReader.Read())
            {
                announcement.Title = Convert.ToString(sdReader["title"]);
                announcement.Athour = Convert.ToString(sdReader["author"]);
                announcement.Content = Convert.ToString(sdReader["content"]);
                announcement.KeyWord = Convert.ToString(sdReader["keywords"]);

            }
            return announcement;
        }



        public bool updateAnnouncement(Announcement announcement, int id)
        {
            bool result = false;
            string commandText = "update Announcement set title=@title,content = @content,keywords = @keywords,author = @author where id=@id";
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("@title", announcement.Title),
               new SqlParameter("@content",announcement.Content),
               new SqlParameter("@keywords",announcement.KeyWord),
               new SqlParameter("@author",announcement.Athour),
               new SqlParameter("@id", id)
            };
            if (SQLHelper.ExecuteNonQuery(commandText, paras)>0)
            {
                result = true;
            }
            return result;
        }


        public bool deleteAnnouncement(int id)
        {
            bool result = false;
            string commandText = "delete  from Announcement  where id=@id";
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("@id", id)
            };
            if (SQLHelper.ExecuteNonQuery(commandText, paras)>0)
            {
                result = true;
            }
            return result;
        }

        public DataSet selectActicleByTitlte(string key)
        {

            string commandText = "select * from Announcement where title= @key";
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("@key", "%"+key+"%")
            };

            return SQLHelper.ExecuteDataSet(commandText, CommandType.Text,paras);

        }

        public DataSet selectActicleByAnthour(string key)
        {
            string commandText = "select * from Announcement where author= @key";
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("@key", "%"+key+"%")
            };
            return SQLHelper.ExecuteDataSet(commandText, CommandType.Text, paras);
        }



        public DataSet selectArticleByReadTime(string key)
        {
            string commandText = "select * from Announcement where readTime= @key";
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("@key", key)
            };
            return SQLHelper.ExecuteDataSet(commandText, CommandType.Text, paras);
        }
    }
}
